
# 基于 Act 的构建

yaml='''
name: spk-online-builder

on:
  push:
    branches:
      - master

jobs:
  act:
    runs-on: ubuntu-20.04
    container: 
      image: debian:latest
      options: --user root -v self-host-workdir/artifacts:/tmp/artifacts

    steps:
      - run: |
          echo "Spk Online Building by Act"
'''

def act_template(docker_image, workdir, commands):
    act_yaml = yaml.replace('debian:latest', docker_image)
    act_yaml = act_yaml.replace('self-host-workdir', workdir)
    prefix = '          '

    # 如果 commands 是字符串，则将其拆分成命令列表
    if isinstance(commands, str):
        commands = commands.strip().split('\n')

    # 遍历 commands 列表，将每个命令添加到 act_yaml 字符串中
    for cmd in commands:
        if len(cmd.strip()) == 0:
            continue
        act_yaml += prefix + cmd.strip() + '\n'
    return act_yaml


if __name__ == '__main__':
    
    cmds = [
        'echo "Hello World"',
        'mkdir /tmp/.spk-online-workdir/mydir'
    ]
    print(act_template('debian:latest', './', cmds))


# name: spk-online-builder

# on:
#   push:
#     branches:
#       - master

# jobs:
#   act:
#     runs-on: ubuntu-20.04
#     container: 
#       image: debian:latest
#       options: --user root -v /tmp/.spk-online-workdir:/tmp/.spk-online-workdir

#     steps:
#       - run: |
#         echo "Spk Online Building by Act"
#         echo "Hello World"
#         mkdir /tmp/.spk-online-workdir/mydir
